var Page={
	init: function() {
		
		apps.request({
			url: urls.ctx + '/task/init',
			success: function(data) {
				console.log(data);
				if(data) {
					if(data.taskGroupList) {
						var groupSelect = '';
						$.each(data.taskGroupList, function(k, v) {
							groupSelect += '<option value="' + v + '">' + v + '</option>';
						});
						$('#s-taskgroup').append(groupSelect);
					}
					$('.page-title').html('任务联系人列表');
					Page.search();
				}
			}
		});
	},
	fromclear: function() {
		$('#f_id').val('');
		$('#f_task_id').val('');
		$('#f_task_name').val('');
		$('#f_task_group').val('');
		$('#f_contact_name').val('');
		$('#f_phone').val('');
		$('#f_email').val('');
		$('#f_wechat').val('');
	},
	search: function() {
		var pageNo = $('#page-demo1').attr('data-page');
		var param = {
			pageNo: pageNo ? pageNo : 1,
			pageSize: 10,
		};
		var taskName = $('#s-taskname').val();
		if(taskName) {
			param.taskName = taskName;
		}
		var taskGroup = $('#s-taskgroup').val();
		if(taskGroup) {
			param.taskGroup = taskGroup;
		}
		var contactName = $('#s-contact-name').val();
		if(contactName) {
			param.contactName = contactName;
		}
		var phone = $('#s-phone').val();
		if(phone) {
			param.phone = phone;
		}
		var email = $('#s-email').val();
		if(email) {
			param.email = email;
		}
		apps.request({
			url: urls.ctx + '/taskhit/query',
			data: param,
			success: function(data) {
				console.log(data);
				if(data) {
					
					if(data.data) {
						var tabltHtml = '';
						$.each(data.data, function(k, v) {
							tabltHtml += '<tr data-id="' + v.id + '">';
							tabltHtml += '<td>' + v.id + '</td>';
							tabltHtml += '<td>' + v.task_id + '</td>';
							tabltHtml += '<td>' + v.task_name + '</td>';
							tabltHtml += '<td>' + v.task_group + '</td>';
							tabltHtml += '<td>' + v.contact_name + '</td>';
							tabltHtml += '<td>' + v.phone + '</td>';
							tabltHtml += '<td>' + v.email + '</td>';
							tabltHtml += '<td>' + v.create_time + '</td>';
							tabltHtml += '<td>' + v.update_time +  '</td>';
							tabltHtml += '<td>';
							tabltHtml += '<div class="btn-group">';
							tabltHtml += '<button type="button" class="btn btn-sm btn-alt datalist-edit">编辑</button>';
							tabltHtml += '<button type="button" class="btn btn-sm btn-alt datalist-cls">查看</button>';
							tabltHtml += '<button type="button" class="btn btn-sm btn-alt datalist-del">删除</button>';
							tabltHtml += '</div>';
							tabltHtml += '</td>';
							tabltHtml += '</tr>';
						});
						$('#tbody-data').html(tabltHtml);
						
						$('.datalist-edit').unbind().bind('click', function() {
							var taskId = $(this).parents('tr').attr('data-id');
							if(taskId) {
								Page.edit(taskId);
							} else {
								
							}
						});
						
						$('.datalist-del').unbind().bind('click', function() {
							var id = $(this).parents('tr').attr('data-id');
							if(id) {
								if(confirm("确定删除?")){
									Page.del(id);
								}	
							} else {
								
							}
						});
					}
					
					
					$("#demo1").jqPaginator({
						totalPages: data.totalpage,
						visiblePages: data.pageSize,
						currentPage: data.page,
						first: '<li class="first"><a href="javascript:void(0);">First<\/a><\/li>',
						prev: '<li class="prev"><a href="javascript:void(0);">Previous<\/a><\/li>',
						next: '<li class="next"><a href="javascript:void(0);">Next<\/a><\/li>',
						last: '<li class="last"><a href="javascript:void(0);">Last<\/a><\/li>',
						page: '<li class="page"><a href="javascript:void(0);">{{page}}<\/a><\/li>',
						onPageChange : function(num, type) {
							console.log(type + '：' + num);
							if (type == 'change') {
								$("#page-demo1").attr('data-page',num);
								Page.search();
							}
						}
					  })
				}
			}
		});
		
		
	},
	edit: function(taskId) {
		apps.request({
			url: urls.ctx + '/taskhit/get',
			data: {id: taskId},
			success: function(data) {
				console.log(data);
				if(data.code == 0) {
					var e = data.entity;
					$('#f_id').val(e.id);
					$('#f_task_id').val(e.taskId);
					$('#f_task_name').val(e.taskName);
					$('#f_task_group').val(e.taskGroup);
					$('#f_contact_name').val(e.contactName);
					$('#f_phone').val(e.phone);
					$('#f_email').val(e.email);
					$('#f_wechat').val(e.wechat);
					
					$('.page-title').html('任务联系人编辑');
					$('#div-task-list').hide();
					$('#div-form').show();
				}
				
			}
		});
	}, 
	del: function(id) {
		apps.request({
			url: urls.ctx + '/taskhit/del',
			data: {id: id},
			success: function(e) {
			  console.log(e);
				if(e.code == 0){
					Page.search();
					
					$('.page-title').html('任务联系人列表');
					$('#div-task-list').show();
					$('#div-form').hide();
				} else {
					alert('保存失败');
				}
			}
		});
	},
	save: function() {
		var parm={
				id:$('#f_id').val(),
				taskId:$('#f_task_id').val(),
				taskName: $('#f_task_name').val(),
				taskGroup:$('#f_task_group').val(),
				contactName: $('#f_contact_name').val(),
				phone:$('#f_phone').val(),
				email:$('#f_email').val(),
				wechat:$('#f_wechat').val(),
		};
		apps.request({
			url: urls.ctx + '/taskhit/save',
			data:parm,
			success: function(e) {
			  console.log(e);
				if(e.code == 0){
					Page.search();
					
					$('.page-title').html('任务联系人列表');
					$('#div-task-list').show();
					$('#div-form').hide();
				} else {
					alert('保存失败');
				}
			}
		});
	},
	
};

$(document).ready(function(){
	//表单返回按钮事件
	$('#f_return').click(function() {
		$('.page-title').html('任务联系人列表');
		$('#div-task-list').show();
		$('#div-form').hide();
	});
	//表单提交按钮事件
	$('#f_submit').click(function() {
		Page.save();
	});
	//列表查询
	$('#s-submit').click(function() {
		Page.search();
	});
	//
	$('#s-add').click(function() {
		Page.fromclear();
		
		$('.page-title').html('任务联系人编辑');
		$('#div-task-list').hide();
		$('#div-form').show();
	});
	
	Page.init();
	
});


